/* 
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research 
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */

/*
 * Created on Jan 17, 2006
 */
package edu.mit.csail.relo.commands;

import org.eclipse.gef.commands.Command;
import org.openrdf.model.URI;

import edu.mit.csail.relo.modelBridge.Artifact;
import edu.mit.csail.relo.parts.AbstractReloRelationPart;
import edu.mit.csail.relo.parts.ArtifactEditPart;
import edu.mit.csail.relo.parts.ReloController;
import edu.mit.csail.relo.parts.NavAidsEditPolicy.BasicRootController;
import edu.mit.csail.relo.parts.NavAidsEditPolicy.NavAidsEditPart;

public final class AddNodeAndRelCmd extends Command {
    private final ReloController rc;
    private final ArtifactEditPart relSrcAEP;
    private final URI property;
    private final Artifact newArt;
    private final boolean fwdRel;

    public AddNodeAndRelCmd(ReloController rc, ArtifactEditPart relSrcAEP, URI property, Artifact newArt, boolean fwdRel) {
        super("Add Node and Rel");
        this.relSrcAEP = (ArtifactEditPart) relSrcAEP;
        this.property = property;
        this.rc = rc;
        this.newArt = newArt;
        this.fwdRel = fwdRel;
    }
    public AddNodeAndRelCmd(BasicRootController rc, NavAidsEditPart aep, URI property, Object newArt, boolean fwdRel) {
        this((ReloController) rc, (ArtifactEditPart) aep, property, (Artifact) newArt, fwdRel);
    }
    public AddNodeAndRelCmd(ReloController rc, Artifact newArt) {
        this((ReloController)rc, (ArtifactEditPart)null, null, newArt, false);
    }

    ArtifactEditPart relationAEP = null;
    AbstractReloRelationPart relPart = null;

    @Override
    public void execute() { 
        relationAEP = rc.createOrFindQueriedArtifactEditPart(newArt);
        if (property != null) {
            if (fwdRel)
                relPart = rc.addRel(relSrcAEP, property, relationAEP);
            else
                relPart = rc.addRel(relationAEP, property, relSrcAEP);
        }
    }

    @Override
    public void undo() { 
        //logger.debug(rel.tag + " Removing " + rel.relationRes + ": " + rel.toString());
        rc.hideRel(relPart);
        if (relationAEP != null)
            rc.removeNode(relationAEP);
    }
}